package services

import (
	"errors"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Models/request"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
)

type ActiveHelp struct{}

func (*ActiveHelp) GetActiveList(claims tools.BaseClaims, paginate *request.HelpActivePage) (data *utils.Page, err error) {

	var account *admin.Account

	var activeList []*admin.ActiveBase

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("无该账号信息")
	}

	err = global.OC_DB.Scopes(dao.HelpActiveDao.GetActiveList(int(account.ID), paginate, global.OC_DB)).Find(&activeList).Offset(-1).Limit(-1).Count(&paginate.Total).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	paginate.Data = activeList

	return &paginate.Page, nil
}

func (*ActiveHelp) GetActive(id int, claims tools.BaseClaims) (data *admin.ActiveBase, err error) {

	var account *admin.Account

	var active *admin.ActiveBase

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("无该账号信息")
	}

	res := global.OC_DB.Where("id = ? AND account_id = ?", id, account.ID).Preload("Cont").First(&active)

	if res.Error != nil {
		return nil, errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return nil, errors.New("找不到该动态信息")
	}

	return active, nil
}

var ActiveHelpService = new(ActiveHelp)
